import 'package:get/get.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';

import '../../isar/isar_key_value_model.dart';
import '../../utils/locale_controller.dart';
import '../../utils/state_controller.dart';

class GetHomeLogic extends GetxController {
  RefreshController refreshController =
      RefreshController(initialRefresh: false);
  var items = <String>[].obs; // 使用 GetX 响应式列表

  var accountValue = ''.obs;
  var passwordValue = ''.obs;

  // 获取状态控制器
  late StateController stateController;


  @override
  void onInit() {
    super.onInit();
    loadInitialData();

    stateController = Get.find();


    if(IsarKeyValueModel.isKeyExist(IsarKeyValueModel.account)){
      IsarKeyValueModel? account =
      IsarKeyValueModel.findModel(IsarKeyValueModel.account);
      accountValue.value= account!.value!;
    }
    if(IsarKeyValueModel.isKeyExist(IsarKeyValueModel.password)){
      IsarKeyValueModel? password =
      IsarKeyValueModel.findModel(IsarKeyValueModel.password);
      passwordValue.value= password!.value!;
    }




    


    // final lc = Get.find<LocaleController>();

    // Future.delayed(const Duration(seconds: 5), () async {
    //   lc.changeLanguage(1);
    // });
  }

  // 加载初始数据
  void loadInitialData() {
    items.assignAll(List.generate(15, (index) => "海外制单进口审批 $index"));
  }

  // 下拉刷新
  void onRefresh() async {
    await Future.delayed(Duration(seconds: 2));
    items.assignAll(List.generate(15, (index) => "(新) 海外制单进口审批 $index"));
    refreshController.refreshCompleted();
  }

  // 上拉加载更多
  void onLoadMore() async {
    await Future.delayed(Duration(seconds: 2));
    items.addAll(
        List.generate(5, (index) => "(更多) 海外制单进口审批 ${items.length + index}"));
    refreshController.loadComplete();
  }

  @override
  void onClose() {
    refreshController.dispose();
    super.onClose();
  }
}
